package com.nsu.evaluation.controller;

import com.nsu.evaluation.common.Const;
import com.nsu.evaluation.common.ServerResponse;
import com.nsu.evaluation.pojo.FeedbackReply;
import com.nsu.evaluation.pojo.Role;
import com.nsu.evaluation.pojo.Student;
import com.nsu.evaluation.pojo.StudentEvaluations;
import com.nsu.evaluation.service.impl.FeedbackReplyServiceImpl;
import com.nsu.evaluation.service.impl.RoleServiceImpl;
import com.nsu.evaluation.service.impl.StudentServiceImpl;
import com.nsu.evaluation.utils.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author EZ
 * 2018-5-1 23:30
 */
@Controller
@RequestMapping("/api/student/")
public class StudentController {

    @Autowired
    private StudentServiceImpl studentServiceImpl;

    @Autowired
    private RoleServiceImpl roleServiceImpl;

    @Autowired
    private FeedbackReplyServiceImpl feedbackReplyServiceImpl;

    /**
     * 学生登录
     * @param username
     * @param password
     * @return
     */
    @RequestMapping(value = "studentLogin")
    @ResponseBody
    public ServerResponse studentLogin(String username, String password,String roleType, HttpSession session){
        ServerResponse<Student> response = studentServiceImpl.studentLogin(username,password);
        if(response.isSuccess()){
            session.setAttribute(Const.CURRENT_USER,response.getData());
            session.setAttribute(Const.CURRENT_USER_ROLE,roleType);
//            Role roleItem = new Role;
//            roleItem.setRoleName(roleType);
//            List<Role> roleList = roleServiceImpl.selectRoleByExample(roleItem).getData();
//            if(null != roleList && roleList.size()>0){
//                session.setAttribute(Const.CURRENT_USER_ROLE,roleList.get(0).getRoleName());
//            }
        }
        return response;
    }

    /**
     * 学生用户注销
     * @param session
     * @return
     */
    @RequestMapping(value = "studentLogout")
    @ResponseBody
    public ServerResponse studentLogout(HttpSession session){
        session.removeAttribute(Const.CURRENT_USER);
        return ServerResponse.createBySuccess("用户注销成功");
    }

    /**
     * 学生修改个人信息
     * @param student
     * @param session
     * @return
     */
    @RequestMapping(value = "学生修改个人信息")
    @ResponseBody
    public ServerResponse<Student> updateStudentInfoByPrimaryKey(Student student,HttpSession session){
        Student currentStudent = (Student)session.getAttribute(Const.CURRENT_USER);
        if(null == currentStudent){
            return ServerResponse.createByErrorMessage("用户尚未登陆");
        }
        //设置学生信息的id为当前登录用户的id
        student.setId(currentStudent.getId());
        return studentServiceImpl.updateStudentInfoByPrimaryKey(student);
    }

    /**
     * 获取学生评价
     * @param session
     * @return
     */
    @RequestMapping(value = "getStudentEvaluations")
    @ResponseBody
    public ServerResponse<StudentEvaluations> getStudentEvaluations(HttpSession session){
        //获取当前学生id，以id来查询评价
        Student student = (Student)session.getAttribute(Const.CURRENT_USER);
        if(null == student){
            return ServerResponse.createByErrorMessage("用户尚未登陆");
        }
        String studentId = student.getId();
        return studentServiceImpl.getStudentEvaluation(studentId);
    }

    /**
     * 提交申诉
     * @param feedbackReply
     * @param session
     * @return
     */
    @RequestMapping(value = "submitFeedbackReply")
    @ResponseBody
    public ServerResponse<FeedbackReply> submitFeedbackReply(FeedbackReply feedbackReply,HttpSession session){
        Student student = (Student)session.getAttribute(Const.CURRENT_USER);
        if(null == student){
            return ServerResponse.createByErrorMessage("用户尚未登陆");
        }
        String studentId = student.getId();
        feedbackReply.setComplaintUser(studentId);
//        feedbackReply.setRoleId();
        return studentServiceImpl.submitFeedbackReply(feedbackReply);
    }

    /**
     * 跳转到学生界面首页
     * @return
     */
    @RequestMapping(value = "toStudentIndex")
    public ModelAndView prepareToLogin(){
        ModelAndView modelAndView = new ModelAndView("/WEB-INF/page/student/studentindex.jsp");
        return modelAndView;
    }

    /**
     * 新增申诉
     * @param feedbackReply
     * @return
     */
    @RequestMapping(value = "addFeedbackReply")
    @ResponseBody
    public ServerResponse addFeedbackReply(FeedbackReply feedbackReply){
        String feedbackReplyId = UUIDUtil.getRandomUUID();
        feedbackReply.setId(feedbackReplyId);
        return feedbackReplyServiceImpl.addFeedbackReply(feedbackReply);
    }
}